﻿using Admin.NET.Application.Entity;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.Extras.Admin.NET;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Admin.NET.Application
{
    /// <summary>
    /// ProjectWorkload服务
    /// </summary>
    [ApiDescriptionSettings("自己的业务", Name = "ProjectWorkload", Order = 100)]
    public class ProjectWorkloadService : IProjectWorkloadService, IDynamicApiController, ITransient
    {
        private readonly IRepository<ProjectWorkload,MasterDbContextLocator> _projectWorkloadRep;
        private readonly IRepository<Project> _projectRep;

        public ProjectWorkloadService(
            IRepository<Project> projectRep,
            IRepository<ProjectWorkload,MasterDbContextLocator> projectWorkloadRep
        )
        {
            _projectRep = projectRep;
            _projectWorkloadRep = projectWorkloadRep;
        }

        /// <summary>
        /// 分页查询ProjectWorkload
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/ProjectWorkload/page")]
        public async Task<PageResult<ProjectWorkloadOutput>> Page([FromQuery] ProjectWorkloadInput input)
        {
            var projectWorkloads = await _projectWorkloadRep.DetachedEntities
                                     .Where(!string.IsNullOrEmpty(input.Description), u => u.Description.ToLower().Contains( input.Description.ToLower()))
                                     .Where(u => u.Month == input.Month)
                                     .Where(u => u.Workload == input.Workload)
                                     .OrderBy(PageInputOrder.OrderBuilder<ProjectWorkloadInput>(input))
                                     .ProjectToType<ProjectWorkloadOutput>()
                                     .ToADPagedListAsync(input.PageNo, input.PageSize);

            return projectWorkloads;
        }

        /// <summary>
        /// 增加ProjectWorkload
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/ProjectWorkload/add")]
        public async Task Add(AddProjectWorkloadInput input)
        {
            var projectWorkload = input.Adapt<ProjectWorkload>();
            await _projectWorkloadRep.InsertAsync(projectWorkload);
        }

        /// <summary>
        /// 删除ProjectWorkload
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/ProjectWorkload/delete")]
        public async Task Delete(DeleteProjectWorkloadInput input)
        {
            var projectWorkload = await _projectWorkloadRep.FirstOrDefaultAsync(u => u.Id == input.Id);
            await _projectWorkloadRep.DeleteAsync(projectWorkload);
        }

        /// <summary>
        /// 更新ProjectWorkload
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("/ProjectWorkload/edit")]
        public async Task Update(UpdateProjectWorkloadInput input)
        {
            var isExist = await _projectWorkloadRep.AnyAsync(u => u.Id == input.Id, false);
            if (!isExist) throw Oops.Oh(ErrorCode.D3000);

            var projectWorkload = input.Adapt<ProjectWorkload>();
            await _projectWorkloadRep.UpdateAsync(projectWorkload,ignoreNullValues:true);
        }

        /// <summary>
        /// 获取ProjectWorkload
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/ProjectWorkload/detail")]
        public async Task<ProjectWorkloadOutput> Get([FromQuery] QueryeProjectWorkloadInput input)
        {
            return (await _projectWorkloadRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id)).Adapt<ProjectWorkloadOutput>();
        }

        /// <summary>
        /// 获取ProjectWorkload列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("/ProjectWorkload/list")]
        public async Task<List<ProjectWorkloadOutput>> List([FromQuery] ProjectWorkloadInput input)
        {
            return await _projectWorkloadRep.DetachedEntities.ProjectToType<ProjectWorkloadOutput>().ToListAsync();
        }    
        
        /// <summary>
        /// 获取Project列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("/ProjectWorkload/fkProject")]
        public async Task<dynamic> FkProjectList()
        {
            var list = await _projectRep.DetachedEntities.ToListAsync();
            return list.Select(e => new {Code = e.Id, Name = e.ProjectName});
        }

    }
}
